package com.earnings.okhttputils.utils.ui.activity.user;

import android.content.Intent;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;

import com.earnings.R;
import com.earnings.okhttputils.utils.OkHttp.OkHttpUtils;
import com.earnings.okhttputils.utils.OkHttp.utils.ToastUtils;
import com.earnings.okhttputils.utils.UrlConstant.HttpUrl;
import com.earnings.okhttputils.utils.bean.CastData;
import com.earnings.okhttputils.utils.bean.UserBankData;
import com.earnings.okhttputils.utils.god.GodLeftHandBaseActivity;
import com.earnings.okhttputils.utils.god.Listener.GodOnClickListener;
import com.earnings.okhttputils.utils.https.HttpArrayCallback;
import com.earnings.okhttputils.utils.https.HttpMap;
import com.earnings.okhttputils.utils.https.HttpObjectCallback;
import com.earnings.okhttputils.utils.ui.dialog.InfoDialog;
import com.earnings.okhttputils.utils.ui.dialog.phoenix.keybottondialog.PswKeyBoardBottomDialog;
import com.earnings.okhttputils.utils.utils.CommonUtil;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.List;
import java.util.Map;

import static com.earnings.R.id.sumbit;


/*
余额-提现
 */

public class UserBalanceWithdrawalsActivity extends GodLeftHandBaseActivity implements GodOnClickListener {

EditText price;
    private int mode=2;
    private String  bankid;
    private InfoDialog info;
    @Override
    protected int onCreateLayoutId() {
        return R.layout.activity_user_balance_withdrawals;
    }

    @Override
    protected void init() {
        setTitle("提现");
        setOnClickListener( this, sumbit,R.id.info,R.id.bank ,R.id.allin,R.id.btn1,R.id.btn2);
        price=getView( R.id.price );
        setText( R.id.balance,"可用余额："+ CommonUtil.getUser().getUser_money()+"元");
        price.addTextChangedListener( new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                if(price.getText().toString().equals( "" ))return;
                float nowprice=Float.parseFloat( price.getText().toString() );
                if(nowprice>Float.parseFloat(CommonUtil.getUser().getUser_money()))
                    price.setText( CommonUtil.getUser().getUser_money() );
                nowprice=Float.parseFloat( price.getText().toString() );
                if(nowprice>49900)
                price.setText( "49900");

            }
        } );
        loadBanks();
        loadBankInfo();
    }



    public boolean checkPrice(){
        String money=price.getText().toString();
        if(money.equals("")){
            ToastUtils.showToast(getContext(),"请输入转入的余额");
            return false;}
        else if(Float.parseFloat( money )<100){
            ToastUtils.showToast(getContext(),"提现至少100元");
            return false;}
        else if(Float.parseFloat( money )%100!=0){
            ToastUtils.showToast(getContext(),"提现金额必须是100的整数倍");
            return false;}
        return true;
    }

    @Override
    public void OnClickListener(View view) {
        int i = view.getId();
        if (i == sumbit) {
            if(checkPrice())sumbit();


        } else if (i == R.id.bank) {
            bundleData.putString( "id",bankid);
            bundleData.putInt( "type",UserEditBankActivity.TYPE_RETURN );
            skipActivity( UserEditBankActivity.class,UserEditBankActivity.TYPE_RETURN);

        } else if (i == R.id.btn1) {
            mode=2;
            setImageDrawable( R.id.checkFirst,R.mipmap.check_true );
            setImageDrawable( R.id.checkSecond,R.mipmap.check_false);
            //   skipActivity( UserEditBankActivity.class );

        }else if (i == R.id.btn2) {
            mode=1;
            setImageDrawable( R.id.checkFirst,R.mipmap.check_false );
            setImageDrawable( R.id.checkSecond,R.mipmap.check_true);
            //   skipActivity( UserEditBankActivity.class );

        } else if (i == R.id.allin) {
            price.setText( CommonUtil.getUser().getUser_money());
       } else if (i == R.id.info) {
           info.setTitle( "提现说明" );
            info.show();

        }
    }

    public void sumbit(){
        final PswKeyBoardBottomDialog pswdialog=new PswKeyBoardBottomDialog(this,price.getText().toString());
        pswdialog.setOnPswKeyBoardResultListener( new PswKeyBoardBottomDialog.OnPswKeyBoardResultListener() {
            @Override
            public void getPswResult(String mResult) {

                Map map=new HttpMap( getContext() );
                map.put( "cardbind_id",bankid );
                map.put( "mode",mode+"");
                map.put( "money",price.getText().toString() );
                map.put( "paypwd",mResult);
                OkHttpUtils.post().params( map ).url( HttpUrl.USER_WITHDRAWALS_URL ).build().execute( new HttpObjectCallback<JSONObject>(getContext()) {
                    @Override
                    public void onSuccess(JSONObject object, String msg) {
                        pswdialog.showSus();
                        bundleData.putString( "bank",getTextView( R.id.bankname ).getText().toString()+"("+getTextView( R.id.bankaccount ).getText().toString()+")" );
                        bundleData.putString( "price",price.getText().toString() );
                        try {
                            bundleData.putString( "id",object.getString( "id" ));
                            bundleData.putString( "msg",object.getString( "msg" ));
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }

                        skipActivity( UserBalanceWithdrawalsResultActivity.class );
                        finish();
                        //  dismissProgress();
                    }

                    @Override
                    public void onFail(int code, String msg) {
                        pswdialog.showFail(msg);
                        //   dismissProgress();
                    }
                } );
            }
        } );

        pswdialog.show();
    }
    public void loadBanks(){
        showProgress();
        Map map=new HttpMap( getContext() );
        OkHttpUtils.post().params( map ).url( HttpUrl.USER_BANK_LOAD_URL ).build().execute( new HttpArrayCallback<UserBankData>(getContext()) {
            @Override
            public void onSuccess(List<UserBankData> object, String msg) {
                if(object.size()!=0){
                    String account=object.get( 0 ).getAccount();
                    bankid=object.get( 0 ).getId();
                    setText(R.id.bankname,object.get( 0 ).getAccount_tname() );
                    setText(R.id.bankaccount,"尾号"+account.substring( account.length()-4,account.length() ));
                    setGlideImagView( R.id.logo, object.get( 0 ).getBank_logo());
                    //seti
                }
                 dismissProgress();
            }

            @Override
            public void onFail(int code, String msg) {
               ToastUtils.showToast( getContext(),msg );
                   dismissProgress();
            }
        } );
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult( requestCode, resultCode, data );
        if(resultCode==UserEditBankActivity.TYPE_RETURN){
            bankid=data.getExtras().getString( "id" );
            setText(R.id.bankname,data.getExtras().getString( "name" ) );
            setText(R.id.bankaccount,"尾号"+data.getExtras().getString( "account" ).substring( data.getExtras().getString( "account" ).length()-4,data.getExtras().getString( "account" ).length() ));
            setGlideImagView( R.id.logo, data.getExtras().getString( "logo" ));

        }
    }

    public void loadBankInfo(){
        showProgress();
        Map map=new HttpMap( getContext() );
        OkHttpUtils.post().params( map ).url( HttpUrl.USER_CAST_INFO_URL ).build().execute( new HttpObjectCallback<CastData>(getContext()) {
            @Override
            public void onSuccess(CastData object, String msg) {
                 info = new InfoDialog(UserBalanceWithdrawalsActivity.this,object.getMsg());
                setText( R.id.ptmsg,object.getPtmsg() );
                setText( R.id.ksmsg,object.getKsmsg() );
                dismissProgress();
            }

            @Override
            public void onFail(int code, String msg) {
                ToastUtils.showToast( getContext(),msg );
                dismissProgress();
            }
        } );
    }

}
